﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CoyoEden;
using System.Collections.Specialized;
using Controls;

public partial class widgets_CategoryHotPosts_edit :WidgetEditBase
{
	protected void Page_Load(object sender, EventArgs e)
	{
	}
	protected override void OnPreRender(EventArgs e)
	{
		if (!Page.IsPostBack) {
			//load current settings
			StringDictionary settings = GetSettings();
			string selectedCategories = null;
			if (settings.ContainsKey("clist"))
			{
				selectedCategories = settings["clist"];
			}
			
			//Assign selected categories
			if (!string.IsNullOrEmpty(selectedCategories))
			{
				CategoryTree cTree = CategorySelector1.FindControl("categorySelector") as CategoryTree;
				HiddenField cTreeHid = CategorySelector1.FindControl("categorySelectorHid") as HiddenField;
				if (cTree != null) {
					cTree.SelectedCategories = selectedCategories;
					cTreeHid.Value = selectedCategories;
				}
			}
			//Assign posts' display number and post title size
			this.txtPostsDisplayNum.Text = settings.ContainsKey("showNum") ? settings["showNum"] : "5";
			this.txtPostTitleLength.Text = settings.ContainsKey("titleSize") ? settings["titleSize"] : "30";

		}
		base.OnPreRender(e);
	}

	#region base implementation
	public override void Save()
	{
		//Get selected categories
		HiddenField cTreeHid = CategorySelector1.FindControl("categorySelectorHid") as HiddenField;
		if (cTreeHid != null)
		{
			var selectedCategories = cTreeHid.Value;
			//save the selected values
			StringDictionary settings = GetSettings();
			settings["clist"] = selectedCategories;
			settings["showNum"] = this.txtPostsDisplayNum.Text;
			settings["titleSize"] = this.txtPostTitleLength.Text;
			SaveSettings(settings);
		}
	}
	#endregion
}
